//
//  ResolveData.m
//  UILesson17_图片异步加载
//
//  Created by lanou3g on 15-3-10.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ResolveData.h"
#import "ASIHTTPRequest.h"
#import "AppDelegate.h"

@implementation ResolveData

+ (void)resolveDataWithUrlStr:(NSString *)urlStr setHTTPMethod:(NSString *)method postBody:(NSString *)postBody resolveBlock:(ResolveBlock)rb
{
    //转成url
    NSURL *url = [NSURL URLWithString:urlStr];
    ASIHTTPRequest __weak*request = [ASIHTTPRequest requestWithURL:url];
    
    //获取全局变量
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
    //设置缓存方式
    [request setDownloadCache:appDelegate.myCache];
    
    //设置缓存数据存储策略，这里采取的是如果无更新或无法联网就读取缓存数据
    [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];

    
    
    [request setCompletionBlock:^{
        NSData *responseData = [request responseData];
        
        id tempObj = [NSJSONSerialization JSONObjectWithData:responseData options:(NSJSONReadingAllowFragments) error:nil];
        
        
        rb(tempObj);
        
        
    }];
    
    
    [request startAsynchronous];
    
    
    
    
}

@end
